package com.foreveross.crawl.workers.listener;

import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

import com.foreveross.crawl.CrawlContext;
import com.foreveross.taskservice.common.bean.TaskModel;
/**
 * 对抓取工作worker的监听
 * @author Administrator
 *
 */
public class DefaultCrawlWorkerListener implements JobListener {
	private static Logger logger = Logger.getLogger(DefaultCrawlWorkerListener.class);
	private static final String START_TIME="START_TIME";
	@Override
	public String getName() {
		return this.getClass().getName();
	}
	/**
	 * job执行前
	 */
	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		TaskModel task = (TaskModel)context.getJobDetail().getJobDataMap().get(CrawlContext.CRAWL_TASK);
		context.getJobDetail().getJobDataMap().put(START_TIME, System.currentTimeMillis());
		logger.info(String.format("开始执行抓取：[%s], 从[%s(%s)]至[%s(%s)], 日期[%s]",
				task.getId(), task.getFromCityName(),task.getFromCity(),task.getToCityName(),task.getToCity(), task.getFlightDate()));
	}

	/**
	 * （Job执行被trigger拒接） 来自官方解释
	 */
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		
	}
	/**
	 * job执行完毕之后
	 */
	@Override
	public void jobWasExecuted(JobExecutionContext context,
			JobExecutionException jobException) {
//		TaskGroup g=(TaskGroup) context.getJobDetail().getJobDataMap().get(CrawlContext.CRAWL_TASK_CHANNEL_GROUP);
		long channelId= context.getJobDetail().getJobDataMap().getLong(CrawlContext.CRAWL_CHANNEL_ID);
		try {
			logger.info(String.format("渠道[%s] 常规任务执行完毕！任务耗时[%s]毫秒",channelId,
					System.currentTimeMillis()-context.getJobDetail().getJobDataMap().getLong(START_TIME)));
		} catch (Exception e) {
			logger.error(e);
		}
	}

	
}
